﻿using System.Text;

namespace 函数
{
    /// <summary>
    /// 1,方法参数的值传递和引用传递
    /// 2,方法的重载：相同的方法名，不同的参数列表。参数列表不同的地方有：参数的个数，参数的类型，参数的顺序。与返回值无关。
    /// 3，三个高级参数 out,ref,params；out 的作用是返回多个值，ref 的作用是传递引用类型的参数，params 的作用是传递可变长度的参数。
    /// 4，位置参数和命名参数
    /// 5，默认参数值
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //func1();
            //string input = Console.ReadLine();
            //int num = getNumber(input);
            //Console.WriteLine(num);

            //string msg = "";
            //bool f = login("yejing1", "1234", out msg);
            //Console.WriteLine(msg);

            //int sum = 0;
            //add(ref sum);
            //Console.WriteLine(sum);

            // // 可变长度的参数列表
            // int s = sum(1, 2, 3, 4,5);
            //
            // // 输出值的参数
            int ss;
            getSum(out ss, 1, 2, 3, 4);
            Console.WriteLine(ss);

            // 传递参数值
            //int sumWithDefault = SumWithDefault(9, 10);
            //Console.WriteLine(Convert.ToString(sumWithDefault));

            // 使用参数的默认值
            //sumWithDefault = SumWithDefault();
            //Console.WriteLine(Convert.ToString(sumWithDefault));

            int max = 0;
            int min = 0;
            int sum = 0;
            int avg = 0;
            int[] nums = { 1, 2, 3, 4, 5 };
            Test(out max, out min, out sum, out avg, nums);
            Console.WriteLine($"max: {max}, min: {min}, sum: {sum}, avg: {avg}");

            String str = "abc";
            int count = str.WordCount();
            Console.WriteLine($"Word count: {count}");

            Console.ReadKey();
        }


        #region 元祖的简单使用
        public static void func1()
        {
            (double, int) t1 = (4.5, 3);
            Console.WriteLine($" tuple with elements {t1.Item1} and {t1.Item2}");

            (int, int, int) t3 = (1, 2, 3);
            Console.WriteLine($" {t3.Item1} {t3.Item2} {t3.Item3} ");

            (double Sum, int Count) t2 = (4.5, 3);
            Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");

            Console.WriteLine(t1.ToString());
        }
        #endregion


        /// <summary>
        /// 值传递，拷贝了一份，不会影响原来的值
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int add(int a, int b)
        {
            return a + b;
        }

        public static double add(double a, double b)
        {
            return a + b;
        }

        /// <summary>
        /// 判断用户的输入是否是数字，
        /// 如果是数字就返回，
        /// 如果不是数字，
        /// 就提示用户重新输入
        /// </summary>
        /// <param name="input"></param>
        public static int getNumber(string input)
        {
            while (true)
            {
                try
                {
                    int num = Convert.ToInt32(input);
                    return num;
                }
                catch
                {
                    input = Console.ReadLine();
                }
            }
        }

        /// <summary>
        /// 计算一个数组的最大值，最小值，平均数，总和
        /// </summary>
        /// <param name="nums"></param>
        /// <param name="max">返回最大值</param>
        /// <param name="min">返回最小值</param>
        /// <param name="sum">返回总和</param>
        /// <param name="avg">返回平均值</param>
        public static void Test(
            out int max, //
            out int min, //
            out int sum, //
            out int avg,
            params int[] nums) //
        {

            // 如果没有传入参数，直接返回
            if (nums.Length == 0)
            {
                max = 0;
                min = 0;
                sum = 0;
                avg = 0;
                return;
            }
            // 计算数组nums的最大值
            max = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                if (nums[i] > max)
                {
                    max = nums[i];
                }
            }

            // 计算数组nums的最小值
            min = nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
                if (nums[i] < min)
                {
                    min = nums[i];
                }
            }
            // 计算数组nums的总和
            sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                sum += nums[i];
            }
            // 计算数组nums的平均值
            avg = 0;
            if (nums.Length > 0)
            {
                avg = sum / nums.Length;
            }
            else
            {
                avg = 0;
            }
        }

        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="flag">返回标志</param>
        /// <param name="msg">返回消息</param>
        public static void login(out bool flag, out string msg)
        {
            flag = true;
            msg = "登陆成功";
        }

        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="msg">输出信息</param>
        /// <returns></returns>
        public static bool login(string name, string pwd, out string msg)
        {
            StringBuilder sb = new StringBuilder();

            bool b1 = false, b2 = false;
            if (name == "yejing")
            {
                b1 = true;
            }
            else
            {
                sb.Append("用户名错误");
            }

            if (pwd == "123")
            {
                b2 = true;
            }
            else
            {
                sb.Append("密码错误");
            }

            if (b1 && b2)
            {
                sb.Append("登陆成功");
            }

            msg = sb.ToString();

            return b1 && b2;
        }

        public static void add(ref int sum)
        {
            sum = 0;
            for (int i = 0; i <= 100; i++)
            {
                sum += i;
            }
        }

        /// <summary>
        /// 可变长度的参数列表
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static int sum(params int[] arr)
        {
            int sum = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
            return sum;
        }

        /// <summary>
        /// 可变长度的参数列表
        /// </summary>
        /// <param name="sum"></param>
        /// <param name="arr"></param>
        public static void getSum(out int sum, params int[] arr)
        {
            sum = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                sum += arr[i];
            }
        }



        /// <summary>
        /// 注意默认值的使用，如果只有一个参数有默认值，那么必须是最后一个
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int SumWithDefault(int a = 10, int b = 10)
        {
            return a + b;
        }
    }


    /// <summary>
    /// 扩展方法,String类的扩展方法
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// 字符串的长度
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int WordCount(this string str)
        {
            return str.Split(new char[] { ' ', '.', '?' },
                             StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }
}
